#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

import ConfigParser
import paramiko
import os
import sys
import time
from retrying import retry


def getTime():
    return time.strftime('%Y%m%d %H:%M:%S', time.localtime())


def countProcess(host, user):
    count = 0
    for line in os.popen("ps xa"):
        line = line.rstrip()
        if line.find("scp " + user + "@" + host) != -1:
            count += 1
    return count


def checkSpace(dev):
    import commands
    import re

    space = 10
    line = commands.getoutput("df -B G -P")
    splitline = line.split("\n")
    for item in splitline:
        if item.startswith(dev):
            item = item.rstrip()
            regex = "^.*? *\d*G *\d*G *(\d*)G *\d*% *.*$"
            match = re.match(regex, item)
            if match:
                result = match.group(1)
                space = int(result)
                break
    return space


@retry
def GetRemoteFilelist(host, user, password, remote_path):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=host, port=22, username=user, password=password)
    filelist = []
    stdin, stdout, stderr = ssh.exec_command("ls " + remote_path)
    for line in stdout:
        line = line.rstrip()
        if line != "":
            filelist.append(line)
    ssh.close()
    return filelist


@retry
def RenameRemoteFile(host, user, password, remote_path, filename, node):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=host, port=22, username=user, password=password)
    ssh.exec_command("mv " + remote_path + "/" + filename + " " + remote_path + "/" + filename + ".node" + node)
    ssh.close()


@retry
def RemoveCompleteSymbol(host, user, password, remote_path, filename):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=host, port=22, username=user, password=password)
    ssh.exec_command("rm -f " + remote_path + "/" + filename)
    ssh.close()


if __name__ == "__main__":
    config = ConfigParser.ConfigParser()
    config.read(sys.argv[1])

    host = config.get("node", "host")
    user = config.get("node", "user")
    password = config.get("node", "password")
    node = config.get("node", "node")
    remote_path = config.get("node", "remote_path")
    dev = config.get("node", "dev")
    max = config.get("node", "max_thread")

    while checkSpace(dev) >= 5:
        if countProcess(host, user) < int(max):
            filelist = GetRemoteFilelist(host, user, password, remote_path)
            exist_file = False
            for filename in filelist:
                if filename.endswith(".tar.gz.complete"):
                    exist_file = True
                    RenameRemoteFile(host, user, password, remote_path, filename, node)
                    print getTime() + " Submit " + filename
                    os.system("python -u downNcheck.py " + sys.argv[1] + " " + filename)
            if not exist_file:
                for filename in filelist:
                    if filename.endswith("all.done"):
                        print("All files downloaded.")
                        RemoveCompleteSymbol(host, user, password, remote_path, filename)
                        sys.exit(0)
        time.sleep(30)
